import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()

Page({
  data: { 
    imgUrl: app.globalData.imgUrl,
    hasNextPage: false,
    pageNo:1,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    list: [], 
  },

  onShow: function () {
    this.getlist(); 
  },
 

  //跳转详情页
  toGoodsDetail(e) {
    var id = e.currentTarget.dataset.id
    console.log(e)
    wx.navigateTo({
      url: '/pages/good/detail/index?id=' + id,
    })
  },
  
  //查询列表
  getlist: function () {
    this.goodsRequest()
  },
 
  //商品请求
  goodsRequest: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/goodsAttend/findCustomerAttend?pageNo=' + this.data.pageNo +'&pageSize=10',
      method: 'POST'
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      this.setData({
        list: list,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });
    })
  },
  
  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //取消关注
  delCustomerAttend: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/goodsAttend/deleteCustomerAttend?gdsId=' + id + '&gdsIdList=' + [id],
      method: 'post'
    }, (res) => { 
      that.getlist();       
    })
  },
  

})